<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                  |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                     |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                           |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;

use AuctionCore\Account\Back\Department;
use \AuctionCore\System\AuctionException;
/**
 * 门店管理
 */
class StoreManageController extends BaseController {

    public function __construct() {
        parent::__construct();
        //页面头部信息
        $head = C('HEAD_INDEX');
        $this->assign("web_title", $head['title']);
        $this->assign("web_keywords", $head['keywords']);
        $this->assign("web_description", $head['description']);
        $this->emp_id = $this->user_info['emp_id'];
        //模板赋值
        $this->assign("emp_id", $this->emp_id);
    }

    public function index() {
        $this->display();
    }

    /**
     * 门店列表
     */
    public function store_list() {
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        //获取list
        $ol = new \AuctionCore\Other\StoreList();
        $result = $ol->getList($page, $size, $params);
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show(); // 分页显示输出
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->display();
    }

    /**
     * 新增/更新门店
     */
    public function add() {
        $data = array();
        $data['store_id'] = I("request.store_id/d");

        if(!IS_POST){
            $store = new \AuctionCore\Other\Store();
            $detail = $store->detail(array('store_id'=>array('eq',$data['store_id'])));
            $depart_detail = Department::getDepartById($detail['depart_id']);
            $detail['depart_name']  = $depart_detail['name'];
            $this->assign('store',$detail);
            $list = Department::getList("id,pid,name");
            // $employees = Employee::get_all();
            $this->assign('list', $list);
            $this->display();
            exit;
        }
        $data['emp_id'] = $this->emp_id;
        $data['store_name'] = I("post.store_name");
        $data['store_lead'] = I("post.store_lead");
        $data['lead_mobile'] = I("post.lead_mobile");
        $data['lead_email'] = I("post.lead_email");
        $data['province'] = I("post.province");
        $data['city'] = I("post.city");
        $data['area'] = I("post.area");
        $data['address'] = I("post.store_address");
        $data['store_tel'] = I("post.store_tel");
        $data['status'] = I("post.store_status");
        $data['createtime'] = date("Y-m-d H:i:s", time());

        $data['type'] = I("post.type",1);
        $data['service'] = I("post.service");
        $data['depart_id'] = I("post.depart_id");
        $data['pointer_x'] = I("post.pointer_x");
        $data['pointer_y'] = I("post.pointer_y");
        $data['qrcode'] = I("post.qrcode");
        $data['script'] = I("post.script");


        if (empty($data['store_name'])) {
            $this->helpJsonResult(400, "门店名称不能为空");
        }
        if (empty($data['store_lead'])) {
            $this->helpJsonResult(400, "门店负责人不能为空");
        }
        if (empty($data['lead_mobile'])) {
            $this->helpJsonResult(400, "负责人手机号不能为空");
        } else {
            $mobile_rules = "/^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|18[0-9]{9}$/";
            if (!preg_match($mobile_rules, $data['lead_mobile'])) {
                $this->helpJsonResult(400, "手机号格式不正确");
            }
        }
        if (empty($data['lead_email'])) {
            $this->helpJsonResult(400, "负责人邮箱不能为空");
        } else {
            $email_rules = "/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/";
            if (!preg_match($email_rules, $data['lead_email'])) {
                $this->helpJsonResult(400, "邮箱格式不正确");
            }
        }
        if (empty($data['province'])) {
            $this->helpJsonResult(400, "门店所在地不能为空");
        }
        if (empty($data['address'])) {
            $this->helpJsonResult(400, "门店详细地址不能为空");
        }
        if (empty($data['store_tel'])) {
            $this->helpJsonResult(400, "门店电话不能为空");
        }
        //开启事务
        M()->startTrans();
        $store = new \AuctionCore\Other\Store();
        $channel = new \AuctionCore\Other\Channel();
        $channel_data = array();
        if($data['store_id']>0){
            $channel_data['from_id'] = I('post.channel_id/d');
            $channel_data['from_name'] =  $data['store_name'];
            $res1 = $channel->edit_channel($channel_data);
            $res2 = $store->update($data,array('store_id'=>array('eq',$data['store_id'])));
            $msg = "门店更新成功！";
        }else{
            $channel_data['pid'] =  6;
            $channel_data['from_name'] =  $data['store_name'];
            $res1 = $channel->add_channel($channel_data);
            $data['channel_id'] = $res1;
            $res2 = $store->add($data);
            $msg = "门店添加成功！";
        }
        if ($res1 !== false && $res2 !== false) {
            M()->commit();
            $this->helpJsonResult(200, $msg);
        } else {
            M()->rollback();
            $this->helpJsonResult(500, $msg);
        }
    }

    /*
     * 图片上传接口
     */

    public function upload_pic() {
        $rep = array('code' => 200);
        $file = $_FILES['qrcode'];
        $flag = true;

//        if(empty(I('data.pic'))){
//            $flag = false;
//            $rep['code'] = 430;
//            $rep['msg'] = '照片上传失败';
//        }
        if ($file['error'] != 0) {
            $flag = false;
            $rep['code'] = 430;
            $rep['msg'] = '照片上传失败';
            \Think\Log::record($rep['msg'] . ',错误码：' . $file['error'] . ',time:' . date('Y-m-d H:i:s'));
        }
        //图片上传
        if ($flag) {
            $object = 'upload/store/' . create_uuid() . '.' . get_extension($file['name']);
            $file_path = $file['tmp_name'];
            try {
                $aliObject = \AuctionCore\Common\Image::upload_file_by_file(OSS_BUCKET, $object, $file_path);
                $host = get_content_cdnurl();
                $rep['data'] = $host . $aliObject;
            } catch (\Exception $e) {
                $rep['code'] = 430;
                $rep['msg'] = $e->getMessage();
            }
        }

        echo json_encode($rep);
        exit;
    }

    /**
     * 禁用/启用
     */
    public function status_post() {
        try {
            $data['store_id'] = I('post.store_id');
            $data['status'] = I('post.store_status');
            $res = M("store")->save($data);
            echo \AuctionCore\Common\Util::auto_jsonp_or_json($_REQUEST, array('code' => 200, 'msg' => ''));
        } catch (AuctionException $e) {
            echo \AuctionCore\Common\Util::auto_jsonp_or_json($_REQUEST, array('msg' => $e->getMessage(), 'code' => $e->getCode()));
            exit;
        }
    }

}
